#导包
from datetime import datetime
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select
class Base_Page:
#初始化对象,并创建driver
    def __init__(self,driver,options=None):
        # if options is None:
        self.driver = driver
        # else:
        #
#打开页面
    def open_url(self,url):
        #获取指定的url
        self.driver.get(url)
#元素定位
    def ele_locator(self,locator,doc):
        ele = None
        #元素等待
        wait_result = self.ele_wait(locator)
        if wait_result:
            print("开始元素%s定位"%str(locator))
            try:
                start_time = datetime.now()
                ele = self.driver.find_element(*locator)
                end_time = datetime.now()
                use_time = (start_time-end_time).seconds
                print("元素%s定位用时%d秒"%(str(locator),use_time))
            except Exception:
                print('元素%s定位失败'%str(locator))
                #如果元素定位失败则截图
                self.scree_shot(doc)
            return ele
        else:
            return("指定元素不存在!!")

#屏幕截图
    def scree_shot(self,filename):
        '''

        :param filename: 截图文件的名称  格式:页面--模块--功能
        :return:
        '''
        path = './test_results/screen_shoot/'+filename+".png"#为截图命名
        self.driver.get_screenshot_as_file(path)

#元素操作之输入
    def ele_input(self,locator,doc,text):
        #定位元素
        ele = self.ele_locator(locator,doc)
        if ele is not None:
            ele.send_keys(text)
        else:
            print("尝试输入内容失败!")

#元素操作之点击
    def ele_click(self,locator,doc):
        ele = self.ele_locator(locator,doc)
        if ele is not None:
            ele.click()
        else:
            print("尝试点击失败!")
        self.scree_shot(doc)
#元素操作之获取文本
    def ele_get_text(self,locator,doc):
        ele = self.ele_locator(locator,doc)
        if ele is not None:
            result = ele.text

        else:
            print("尝试点击失败!")
            result = None
        return result
#元素等待
    def ele_wait(self,locator,wait_time=10,lun_xun=1):
        print("开始等待元素%s"%str(locator))
        try:
            WebDriverWait(self.driver,wait_time,lun_xun).until(EC.visibility_of_element_located(locator))
            print("元素已出现")
            return True
        except Exception:
            print("未找到该元素!")
            return False

#切换widows
    def switch_to_windows(self,index=-1):
        #获取所有页面句柄
        handles =self.driver.window_handles
        self.driver.switch_to(handles[index])#默认切换至最后一个窗口
#切入frame
    def switch_into_frame(self,locator,doc):
        ele = self.ele_locator(locator,doc) #定位框架
        if ele is not None:
            self.driver.switch_to(ele)
        else:
            print("没有定位到%s框架!"%ele)
# 切出框架
    def switch_out_frame(self,locator,doc):
        ele = self.ele_locator(locator,doc) #定位框架
        if ele is not None:
            self.driver.switch_to(ele)
        else:
            print("没有定位到%s框架!"%ele)


#切换弹窗处理
    def switch_to_alter(self,actions="a",text=None):

        alter = self.driver.switch_to.alert
        if actions=='a':

            alter.accept()
        elif actions=="d":
            alter.dismiss()
        else:
            alter.send_keys(text)
            alter.accept()
#选择框选择
    def select_someone(self,locator,doc,index=None,value=None,visible_text=None):
        #被选框定位
        ele = self.ele_locator(locator,doc)
        select = Select(ele)
        #根据index选择
        if index is not None:
            select.select_by_index(index)
            return
        #根据选项的value属性的值选择
        if value is not None:
            select.select_by_value(value)
            return
        #根据visibe_text选择
        if visible_text is not None:
            select.select_by_visible_text(visible_text)
            return
#获取属性值
    def ele_get_attribute(self,locator,doc,a_name):
        '''
        :param locator:
        :param doc:
        :param a_name: 需要获取的属性值的属性名称
        :return:
        '''
        #定位元素
        ele = self.ele_locator(locator,doc)
        #获取属性值
        ele.get_attribute(a_name)
#通过JS脚本删除属性值
    def ele_remove_attribute(self,locator, doc,a_name):
        '''

        :param locator:
        :param doc:
        :param a_name: 想要移除的属性名
        :return:
        '''
        # 定位元素
        ele = self.ele_locator(locator, doc)
        #组装JS脚本
        js = 'arguments[0].removeAttribute"%s"'%a_name
        #执行JS脚本移除指定元素的指定属性
        self.driver.execute_script(js,ele)
#执行JS脚本
    def ele_excute_JS(self,js,ele=None):
        self.driver.execute_script(js,ele)
#页面操作后退
    def page_back(self):
        self.driver.back()
    def page_refresh(self):
        self.driver.refresh()
    def page_forward(self):
        self.driver.forward()
    def driver_quit(self):
        self.driver.quit()